package micropascalcomp.FSA;

import java.io.*;
import micropascalcomp.MP_Token;
import micropascalcomp.Token;

/**
 * The FSA wrapper class for the method that handles finding the L_than token
 * @author Jason Welch, Codi Gann, Jamal Martin
 */
public class LThanFSA implements FSABehavior {

    public MP_Token getToken(FileReader file, LineNumberReader inFile, int colNum) {
        MP_Token result = null;     //token class to be returned
        int readChar;
        String lexeme = "";         //var to hold the lexeme string

        try {
            readChar = inFile.read();           //grab the first char for the token
            lexeme = "" + (char) readChar;      //add it to the lexeme string
            inFile.mark(colNum);                //mark the current place for FSA

            readChar = inFile.read();                   //grab the 2nd char for the token
            if (readChar == 61) {                       //if a equals sign is found
                lexeme = lexeme + (char) readChar;      //add it to the lexeme string
                //create token class
                result = new MP_Token(Token.MP_LEQUAL, lexeme, inFile.getLineNumber(), colNum);

            } else if (readChar == 62) {                //if a gthan sign is found
                lexeme = lexeme + (char) readChar;      //add it to the lexeme string
                //create token class
                result = new MP_Token(Token.MP_NEQUAL, lexeme, inFile.getLineNumber(), colNum);
            } else {
                if (readChar == 10 || readChar == 13) {   //check if token was a \n
                    inFile.setLineNumber(inFile.getLineNumber() - 1);
                }
                //create token class
                inFile.reset();
                result = new MP_Token(Token.MP_LTHAN, lexeme, inFile.getLineNumber(), colNum);
            }
        } catch (IOException e) {
            System.err.println(e);
        }

        return result;
    }
}
